package java_demos.java_io.read_file;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class ReadFileBufferedInputStream {

    public static void main(String[] args) {
        // 通过路径获取文件对象
        File file = new File(System.getProperty("user.dir") + File.separator+"create_file_java7.txt");
        // 定义文件输入流
        FileInputStream fileInputStream;
        fileInputStream = null;
        //定义bufferedInputStream缓冲流
        BufferedInputStream bufferedInputStream;
        bufferedInputStream = null;
        // 定义数据输入流
        DataInputStream dataInputStream;
        dataInputStream = null;
        try  {
            fileInputStream = new FileInputStream(file);
            // 创建BufferedInputStream对象，并指定缓冲区大小
            bufferedInputStream = new BufferedInputStream(fileInputStream, 1024);
            // 创建DataInputStream对象，用于读取数据
            dataInputStream = new DataInputStream(bufferedInputStream);
            // 读取数据
            String line;
            while ((line = dataInputStream.readLine())!= null) {
                System.out.println(line);
            }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (dataInputStream!= null) {
                    dataInputStream.close();
                }
                if (bufferedInputStream!= null) {
                    bufferedInputStream.close();
                }
                if (fileInputStream!= null) {
                    fileInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
